package com.example.administrator.liveapp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.AllOrdersBookRecyclerAdapter;
import com.example.administrator.liveapp.adapter.ReservationViewPagerAdapter;
import com.example.administrator.liveapp.bean.AllOrdersBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;
import com.zhy.view.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 全部订单activity
 */
public class AllOrdersPage extends AppCompatActivity implements View.OnClickListener{

    private RecyclerView AllOrders_tab1_book_Recycle;//全部订单
    private AllOrdersBean bean;
    private TextView no_orders;
    private Window window;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allorders_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("action.refreshFriend");
        registerReceiver(mRefreshBroadcastReceiver, intentFilter);
        initView();
        setData();
    }

    private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("action.refreshFriend"))
            {
                setData();
            }
        }
    };

    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mRefreshBroadcastReceiver);
    }

    private void setData() {
        String url = AppUrl.URL+AppUrl.OrderURL;
        Map<String,String> map = new HashMap<>();
        map.put("consumerId",ApplicationUtils.getInstance().loginBean.getConsumerid());
        XUtils.Get(url,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result, AllOrdersBean.class);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                if(bean.getBookOrder()!=null){
                    reservationRecyclerView(bean.getBookOrder());
                    no_orders.setVisibility(View.GONE);
                    AllOrders_tab1_book_Recycle.setVisibility(View.VISIBLE);
                }else{
                    no_orders.setVisibility(View.VISIBLE);
                    AllOrders_tab1_book_Recycle.setVisibility(View.GONE);
                }
            }
        });
    }

    protected void initView() {
        AllOrders_tab1_book_Recycle = (RecyclerView)findViewById(R.id.AllOrders_tab1_book_Recycle);
        no_orders = (TextView) findViewById(R.id.no_orders);
        findViewById(R.id.allorders_returns).setOnClickListener(this);
    }

    private void reservationRecyclerView(List<AllOrdersBean.BookOrderBean> bookOrder) {
        //全部订单(图书)AllOrders_tab1_book_Recycle
        FullyLinearLayoutManager tab1_linerLayoutManager2 = new FullyLinearLayoutManager(this);
        tab1_linerLayoutManager2.setScrollEnabled(true);
        AllOrders_tab1_book_Recycle.setHasFixedSize(true);
        AllOrders_tab1_book_Recycle.setLayoutManager(tab1_linerLayoutManager2);
        AllOrdersBookRecyclerAdapter tab1_book = new AllOrdersBookRecyclerAdapter(bookOrder);
        AllOrders_tab1_book_Recycle.setAdapter(tab1_book);
        tab1_book.setOnItemClickListener(new AllOrdersBookRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(AllOrdersPage.this,OrderDetails.class);
                intent.putExtra("goodsOrder",bean.getBookOrder().get(position).getGoodsOrder());
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.allorders_returns:
                finish();
                break;
        }
    }
}
